package com.xnyzc.lhy.finance.service.impl.invoice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.esotericsoftware.minlog.Log;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.finance.entity.invoice.OaSysInvoiceApply;
import com.xnyzc.lhy.finance.entity.invoice.OaSysInvoiceDetail;
import com.xnyzc.lhy.finance.entity.invoice.OaSysInvoiceEmailSend;
import com.xnyzc.lhy.finance.entity.invoice.OaSysInvoiceInfo;
import com.xnyzc.lhy.finance.mapper.invoice.OaSysInvoiceApplyMapper;
import com.xnyzc.lhy.finance.mapper.invoice.OaSysInvoiceDetailMapper;
import com.xnyzc.lhy.finance.mapper.invoice.OaSysInvoiceEmailSendMapper;
import com.xnyzc.lhy.finance.mapper.invoice.OaSysInvoiceInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.finance.service.invoice.IOaSysInvoiceInfoService;
import com.xnyzc.lhy.mis.entity.cms.OaSysRoleMenu;
import com.xnyzc.lhy.mis.entity.system.OaSysUserRole;
import com.xnyzc.lhy.order.entity.user.wallet.OaCWallet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 发票表 服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-10-24
 */
@Slf4j
@Service
public class OaSysInvoiceInfoServiceImpl extends ServiceImpl<OaSysInvoiceInfoMapper, OaSysInvoiceInfo> implements IOaSysInvoiceInfoService {

    @Autowired
    OaSysInvoiceApplyMapper oaSysInvoiceApplyMapper;
    @Autowired
    OaSysInvoiceInfoMapper oaSysInvoiceInfoMapper;
    @Autowired
    OaSysInvoiceDetailMapper oaSysInvoiceDetailMapper;
    @Autowired
    OaSysInvoiceEmailSendMapper oaSysInvoiceEmailSendMapper;

    /**
     * 创建发票
     * */
    @Override
    @Transactional
    public Rv createInvoiceInfo(OaSysInvoiceApply oaSysInvoiceApply, OaSysInvoiceInfo oaSysInvoiceInfo, OaSysInvoiceDetail oaSysInvoiceDetail) {
        try {
            Date date = new Date();
            Long sysUserId = new Long("781526796107776");

            Long oaSysInvoiceApplyId = IDUtil.nextId();
            oaSysInvoiceApply.setInvoiceApplyId(oaSysInvoiceApplyId);

            oaSysInvoiceApply.setCreateUser(sysUserId);
            oaSysInvoiceApply.setCreateTime(date);
            oaSysInvoiceApply.setUpdateTime(date);

            oaSysInvoiceApplyMapper.insert(oaSysInvoiceApply);
            Long oaSysInvoiceInfoId = IDUtil.nextId();
            oaSysInvoiceInfo.setInvoiceInfoId(oaSysInvoiceInfoId);
            oaSysInvoiceInfo.setInvoiceApplyId(oaSysInvoiceApply.getInvoiceApplyId());
            //开票中
            oaSysInvoiceInfo.setInvoiceStatus("1");

            oaSysInvoiceInfo.setCreateUser(sysUserId);
            oaSysInvoiceInfo.setCreateTime(date);
            oaSysInvoiceInfo.setUpdateTime(date);
            oaSysInvoiceInfoMapper.insert(oaSysInvoiceInfo);
            Long oaSysInvoiceDetailId = IDUtil.nextId();
            oaSysInvoiceDetail.setInvoiceDetailId(oaSysInvoiceDetailId);
            oaSysInvoiceDetail.setInvoiceInfoId(oaSysInvoiceInfo.getInvoiceInfoId());

            oaSysInvoiceDetail.setCreateUser(sysUserId);
            oaSysInvoiceDetail.setCreateTime(date);
            oaSysInvoiceDetail.setUpdateTime(date);
            oaSysInvoiceDetailMapper.insert(oaSysInvoiceDetail);
            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
    *   开票成功操作，根据invoiceApplyId更新OaSysInvoiceInfo，具体更新字段
     *  invoiceCode，invoiceNo，invoiceDate，taxControlCode，invoiceCheckCode，invoiceQrCode
    *
    * */
    @Override
    @Transactional
    public Rv billingSuccessfulOperation(OaSysInvoiceInfo oaSysInvoiceInfo) {

        OaSysInvoiceInfo oaSysInvoiceInfo1 = null;
        try {
            oaSysInvoiceInfo1 = oaSysInvoiceInfoMapper.selectById(oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceInfo1.setInvoiceCode(oaSysInvoiceInfo.getInvoiceCode());
            oaSysInvoiceInfo1.setInvoiceNo(oaSysInvoiceInfo.getInvoiceNo());
            oaSysInvoiceInfo1.setInvoiceDate(oaSysInvoiceInfo.getInvoiceDate());
            oaSysInvoiceInfo1.setTaxControlCode(oaSysInvoiceInfo.getTaxControlCode());
            oaSysInvoiceInfo1.setInvoiceCheckCode(oaSysInvoiceInfo.getInvoiceCheckCode());
            oaSysInvoiceInfo1.setInvoiceQrCode(oaSysInvoiceInfo.getInvoiceQrCode());
            oaSysInvoiceInfo1.setInvoiceStatus(oaSysInvoiceInfo.getInvoiceStatus());

            oaSysInvoiceInfoMapper.updateById(oaSysInvoiceInfo1);
            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
     *
     * 开票失败操作。根据invoiceApplyId更新OaSysInvoiceInfo，具体更新字段
     * invoiceStatus，applyFailReason
     * */
    @Override
    public Rv billingFailedOperation(OaSysInvoiceInfo oaSysInvoiceInfo) {
        OaSysInvoiceInfo oaSysInvoiceInfo1 = null;
        try {
            oaSysInvoiceInfo1 = oaSysInvoiceInfoMapper.selectById(oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceInfo1.setInvoiceStatus(oaSysInvoiceInfo.getInvoiceStatus());
            oaSysInvoiceInfo1.setApplyFailReason(oaSysInvoiceInfo.getApplyFailReason());
            oaSysInvoiceInfoMapper.updateById(oaSysInvoiceInfo1);
            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
     * 发票作废，更新根据invoiceApplyId更新OaSysInvoiceInfo，更新invoiceStatus
     *
     * */
    @Override
    public Rv invoiceInvalid(OaSysInvoiceInfo oaSysInvoiceInfo) {
        OaSysInvoiceInfo oaSysInvoiceInfo1 = null;
        try {
            oaSysInvoiceInfo1 = oaSysInvoiceInfoMapper.selectById(oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceInfo1.setInvoiceStatus(oaSysInvoiceInfo.getInvoiceStatus());
            oaSysInvoiceInfoMapper.updateById(oaSysInvoiceInfo1);
            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
     *  发票发送记录创建，创建OaSysInvoiceEmailSend
     * */
    @Override
    public Rv InvoiceSendingRecordCreation(OaSysInvoiceEmailSend oaSysInvoiceEmailSend) {

        try {
            Long id = IDUtil.nextId();
            oaSysInvoiceEmailSend.setInvoiceSendId(id);
            oaSysInvoiceEmailSendMapper.insert(oaSysInvoiceEmailSend);
            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
     * 查询OaSysInvoiceInfo by id
     * */
    @Override
    public Rv selectOaSysInvoiceInfoById(Long invoiceInfoId) {

        try {
            OaSysInvoiceInfo oaSysInvoiceInfo = oaSysInvoiceInfoMapper.selectById(invoiceInfoId);
            return Rv.wrap(EErrorCode.success,oaSysInvoiceInfo);
        } catch (Exception e) {
            log.error("{}",e);
        }
        return Rv.wrap(EErrorCode.noData);
    }
    /**
     * 根据  invoiceInfoId 删除
     *     OaSysInvoiceApply;
     *     OaSysInvoiceInfo;
     *     OaSysInvoiceDetail;
     *     OaSysInvoiceEmailSend;
     * */
    @Override
    @Transactional
    public Rv deletOaSysInvoiceById(Long invoiceApplyId) {
        try {
            oaSysInvoiceApplyMapper.deleteById(invoiceApplyId);

            OaSysInvoiceInfo oaSysInvoiceInfo = oaSysInvoiceInfoMapper.selectOne(Qw.create().eq(OaSysInvoiceInfo.INVOICE_APPLY_ID,invoiceApplyId));
            oaSysInvoiceInfoMapper.deleteById(oaSysInvoiceInfo.getInvoiceInfoId());

            QueryWrapper<OaSysInvoiceDetail> deleteQueryWrapper = new QueryWrapper<>();
            deleteQueryWrapper.eq(OaSysInvoiceDetail.INVOICE_INFO_ID,oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceDetailMapper.delete(deleteQueryWrapper);

            QueryWrapper<OaSysInvoiceEmailSend> oaSysInvoiceEmailSendWapper = new QueryWrapper<>();
            oaSysInvoiceEmailSendWapper.eq(OaSysInvoiceDetail.INVOICE_INFO_ID,oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceEmailSendMapper.delete(oaSysInvoiceEmailSendWapper);

            return Rv.wrap(EErrorCode.success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.noData);
    }

    /**
     * 根据申请id获取发票信息
     * */
    @Override
    public OaSysInvoiceInfo selectOaSysInvoiceInfoByApplyId(Long applyId) {
        try {
            //获取审核状态字典
            QueryWrapper queryWrapper=new QueryWrapper();

            queryWrapper.eq(OaSysInvoiceInfo.INVOICE_APPLY_ID, applyId);
            OaSysInvoiceInfo oaSysInvoiceInfo = oaSysInvoiceInfoMapper.selectOne(queryWrapper);
            return oaSysInvoiceInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Rv billingFarmatFileOperation(OaSysInvoiceInfo oaSysInvoiceInfo) {
        try {
            OaSysInvoiceInfo oaSysInvoiceInfo1 = oaSysInvoiceInfoMapper.selectById(oaSysInvoiceInfo.getInvoiceInfoId());
            oaSysInvoiceInfo1.setInvoiceStatus(oaSysInvoiceInfo.getInvoiceStatus());
            oaSysInvoiceInfo1.setFileUrl(oaSysInvoiceInfo.getFileUrl());
            oaSysInvoiceInfoMapper.updateById(oaSysInvoiceInfo1);
            return Rv.wrap(EErrorCode.success);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
